package com.shop2cn.iapi.sqmarketing.testcase;

import java.math.BigDecimal;
import java.util.Arrays;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.sqmarketing.parameter.OffShelvesByIdReq;
import com.shop2cn.iapi.sqmarketing.pojo.ActivityConditionItemPojo;
import com.shop2cn.iapi.sqmarketing.service.OffShelvesByIdCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.shenqi.SqmarketingService;
import com.ymttest.database.model.sqmarketing.MarketingActivity;
import com.ymttest.database.sqlwapper.sqmarketing.MarketingActivityWrapper;

public class Ts_OffShelvesById {

	// java接口
	private static OffShelvesByIdReq offShelvesByIdReq;
	private static OffShelvesByIdCall offShelvesByIdCall;
	
	private static MarketingActivityWrapper marketingActivityWrapper = new MarketingActivityWrapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据活动id下架活动");
	}

	@Before
	public void caseUp() {
		offShelvesByIdReq = new OffShelvesByIdReq();
		offShelvesByIdCall = new OffShelvesByIdCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(offShelvesByIdCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verifyDb() throws Exception {
		
		MarketingActivity actual = marketingActivityWrapper
				.selectByPrimaryKey(offShelvesByIdReq.getId());

		Logger.verifyEquals((byte) 2, actual.getActivity_status(),
				"验证Activity_status");
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_OffShelvesById_001() {
		Logger.start(true, "下架活动");

		try {
			
			// 删除之前的营销活动，避免条件重复报错
			SqmarketingService.deleteAllMarketingActivity(1234L);

			// 清除营销活动缓存
			SqmarketingService.deleteCache();

			// 创建满金额赠优惠条件
			ActivityConditionItemPojo activityConditionItem = new ActivityConditionItemPojo();

			activityConditionItem.setConditionType(1);
			activityConditionItem.setItemList(Arrays.asList("ALL"));

			// 创建满金额赠优惠活动
			Long id = SqmarketingService.saveOrUpdaateMarketingActivity(2,
					"CNY", BigDecimal.ONE, 1234L, 1,
					Arrays.asList(activityConditionItem));

			offShelvesByIdReq.setId(id);
			offShelvesByIdReq.setSellerId(1234L);
			offShelvesByIdReq.setActivityStatus(2);

			offShelvesByIdCall.setData(offShelvesByIdReq);
			offShelvesByIdCall.callService();

			Logger.verifyEquals(true, offShelvesByIdCall.getSuccess(),
					"验证success");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
